package com.hitplane.core 
{
	import com.hitplane.interfaces.IDisplayObject;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	/**
	 * @describe  	...
	 * @author  	rrDog
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		... 
	 */
	public class BaseDisplay extends Sprite implements IDisplayObject
	{
		//显示对象类型
		private var _type:String;
		//存储各种显示状态
		private var _stateArr:Array;
		public function get stateArr():Array {return _stateArr;}
		public function get type():String {return _type;}
		public function set type(str:String):void {_type = str;}
		//public===============================================================
		/* INTERFACE com.hitplane.interfaces.IDisplayObject */
		/**
		 * 注册显示对象的状态
		 * @param	state
		 * @param	mc
		 */
		public function registerState(state:String, mc:DisplayObject):void 
		{
			if (_stateArr == null) {
				_stateArr = new Array();
			}
			if (mc == null) throw new Error(this+"：registerState:不存在的显示对象");
			_stateArr[state] = _stateArr[state]||mc;
			if(mc is MovieClip)MovieClip(mc).gotoAndStop(1);
		}
		/**
		 * 设置显示对象的状态
		 * @param	state
		 */
		public function setShowState(state:String):void 
		{
			clearDisplay();
			if (_stateArr!=null&&_stateArr[state] != null) {
				this.addChild(_stateArr[state]);
				if(_stateArr[state] is MovieClip)_stateArr[state].gotoAndPlay(1);
			}else {
				throw new Error(this+"：setState:_stateArr未定义或者不存在的显示对象");
			}
		}
		/**
		 * 释放内存
		 */
		public function dispose():void 
		{
			for(var i:String in _stateArr){
				_stateArr[i]=null;
			}
			_stateArr=null;
			disposeDisplay(this);
			if (this.parent) parent.removeChild(this);
		}
		
		//private===============================================================
		/**
		 * 移除现有显示对象
		 * 
		 */
		private function clearDisplay():void{
			var childNum:int = this.numChildren;
			for(var j:int;j<childNum;j++){
				this.removeChildAt(0);
			}
		}
		/**
		 * 
		 * 释放指定显示对象容器中的所有显示对象
		 * @param displayObj
		 * 
		 */
		private function disposeDisplay(displayObj:DisplayObjectContainer):void {
			var childNum:int = displayObj.numChildren;
			var child:DisplayObject;
			for(var j:int;j<childNum;j++){
				child = displayObj.removeChildAt(0);
				if(child is DisplayObjectContainer)disposeDisplay(child as DisplayObjectContainer)
			}
		}
	}
	
}